﻿using System;
using System.Windows.Input;

namespace Common
{
    /// <summary>
    /// 实现ICommand接口
    /// </summary>
    public class CommandBase : ICommand
    {
        /// <summary>
        /// 是否可执行
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// 判定命令是否可执行
        /// </summary>
        /// <param name="parameter"> </param>
        /// <returns> </returns>
        public bool CanExecute(object parameter)
        {
            if (DoCanExecute == null)
            {
                return true;
            }

            return DoCanExecute.Invoke(parameter) == true;
        }

        /// <summary>
        /// ctor
        /// </summary>
        public CommandBase()
        {
        }

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="action"> </param>
        public CommandBase(Action<object> action)
        {
            DoExecute = action;
        }

        /// <summary>
        /// 执行委托
        /// </summary>
        /// <param name="parameter"> </param>
        public void Execute(object parameter)
        {
            DoExecute?.Invoke(parameter);
        }

        /// <summary>
        /// 委托
        /// </summary>
        public Action<object> DoExecute { get; set; }

        /// <summary>
        /// 委托
        /// </summary>
        public Func<object, bool> DoCanExecute { get; set; }
    }
}